package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

import javax.swing.JLabel;
import javax.swing.JTextField;

public class RandomTest extends JFrame {

	private JPanel contentPane;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JButton btnNewButton;
	private JButton button;
	private JButton button_1;
	private JButton btnLotto;
	private JButton btnLotto_1;
	private JLabel lblNewLabel;
	private JTextField nu1;
	private JTextField nu2;
	private JTextField nu3;
	private JTextField nu4;
	private JTextField nu5;
	private JTextField nu6;
	private JButton btnClear;
	private JButton btnLotto_2;
	private JButton btnLotto_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RandomTest frame = new RandomTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RandomTest() {
		setTitle("RandomTest");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getScrollPane());
		contentPane.add(getBtnNewButton());
		contentPane.add(getButton());
		contentPane.add(getButton_1());
		contentPane.add(getBtnLotto());
		contentPane.add(getBtnLotto_1());
		contentPane.add(getLblNewLabel());
		contentPane.add(getNu1());
		contentPane.add(getNu2());
		contentPane.add(getNu3());
		contentPane.add(getNu4());
		contentPane.add(getNu5());
		contentPane.add(getNu6());
		contentPane.add(getBtnClear());
		contentPane.add(getBtnLotto_2());
		contentPane.add(getBtnLotto_3());
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 10, 410, 106);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}

	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("1~10");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int r = (int) (Math.random()/* 이게x */* 10) + 1; // 이게
																	// 공식(최대값-최소값)+1   x->(최대-최소)+1->최소
					result.append("1~10 : " + r);
					result.append("\n");
				}
			});
			btnNewButton.setBounds(12, 196, 85, 23);
		}
		return btnNewButton;
	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton("20~80");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int r = (int) (Math.random() * 61) + 20;
					result.append("20~80 : " + r);
					result.append("\n");
				}
			});
			button.setBounds(109, 196, 85, 23);
		}
		return button;
	}

	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("1~45");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int r = (int) (Math.random() * 45) + 1;
					result.append("1~45 : " + r);
					result.append("\n");
				}
			});
			button_1.setBounds(206, 196, 85, 23);
		}
		return button_1;
	}

	private JButton getBtnLotto() {
		// 1~45사지의 수중 중복없이 6개 수 추출
		// 발생 순서에 상관없이 오름차순 정렬.
		// 행운의 수를 1~6개까지 정할 수 있게 하고 정해진 행운의 수는 무조건 추출될 수 있도록 조치.
		if (btnLotto == null) {
			btnLotto = new JButton("Lotto");
			btnLotto.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int[] Lotto = new int[6];
					result.append("--------------------------\n");
					for (int k = 0; k < 5; k++) {
						for (int i = 0; i < Lotto.length; i++) {    //length =6
							Lotto[i] = (int) (Math.random() * 45) + 1;
						for (int j = i - 1; j >= 0; j--) {
								if (Lotto[i] == Lotto[j]) {
									i--;
									break;
								}
							}
						}

						Arrays.sort(Lotto);
						result.append(Arrays.toString(Lotto));
						result.append("\n");
					}
				}

			});
			btnLotto.setBounds(303, 196, 85, 23);
		}
		return btnLotto;
	}

	private JButton getBtnLotto_1() {
		if (btnLotto_1 == null) {
			btnLotto_1 = new JButton("Lotto2");
			btnLotto_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int[] base = new int[45];
					int[] lo = new int[6];
					int temp = 0;
					for (int i = 1; i < 46; i++)
						base[i - 1] = i;
					for (int i = 0; i < 1000; i++) {
						int k = (int) (Math.random() * 44) + 1;
						temp = base[0];
						base[0] = base[k];
						base[k] = temp;

					}
					System.arraycopy(base, 0, lo, 0, 6);
					Arrays.sort(lo);
					result.append(Arrays.toString(lo));
					result.append("\n");
				}
			});
			btnLotto_1.setBounds(12, 229, 85, 23);
		}
		return btnLotto_1;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("행운의 수");
			lblNewLabel.setBounds(12, 128, 57, 15);
		}
		return lblNewLabel;
	}

	private JTextField getNu1() {
		if (nu1 == null) {
			nu1 = new JTextField();
			nu1.setBounds(12, 153, 57, 21);
			nu1.setColumns(10);
		}
		return nu1;
	}

	private JTextField getNu2() {
		if (nu2 == null) {
			nu2 = new JTextField();
			nu2.setColumns(10);
			nu2.setBounds(81, 153, 57, 21);
		}
		return nu2;
	}

	private JTextField getNu3() {
		if (nu3 == null) {
			nu3 = new JTextField();
			nu3.setColumns(10);
			nu3.setBounds(150, 153, 57, 21);
		}
		return nu3;
	}

	private JTextField getNu4() {
		if (nu4 == null) {
			nu4 = new JTextField();
			nu4.setColumns(10);
			nu4.setBounds(219, 153, 57, 21);
		}
		return nu4;
	}

	private JTextField getNu5() {
		if (nu5 == null) {
			nu5 = new JTextField();
			nu5.setColumns(10);
			nu5.setBounds(288, 153, 57, 21);
		}
		return nu5;
	}

	private JTextField getNu6() {
		if (nu6 == null) {
			nu6 = new JTextField();
			nu6.setColumns(10);
			nu6.setBounds(357, 153, 57, 21);
		}
		return nu6;
	}

	private JButton getBtnClear() {
		if (btnClear == null) {
			btnClear = new JButton("clear");
			btnClear.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					result.setText("");
				}
			});
			btnClear.setBounds(303, 229, 85, 23);
		}
		return btnClear;
	}

	private JButton getBtnLotto_2() {
		if (btnLotto_2 == null) {
			btnLotto_2 = new JButton("Lotto3");
			btnLotto_2.addActionListener(new ActionListener() {

				// 행운의 수 를 직접 입력하고 나머지를 구하시오.
				public void actionPerformed(ActionEvent arg0) {
					int[] a = new int[6];
					int c = 0;

					try {
						for (int i = 0; i <= 0; i++) {
							if (!nu1.getText().equals("")) {
								int aa = Integer.parseInt(nu1.getText());
								if (aa > 0 && aa < 46) {
									a[c] = aa;
								} else {
									JOptionPane.showMessageDialog(null,
											"1~45의수를 입력");
									break;
								}
								c++;
							}
							if (!nu2.getText().equals("")) {
								int bb = Integer.parseInt(nu2.getText());
								if (bb > 0 && bb < 46 && !nu2.getText().equals(nu1.getText())) {
									a[c] = bb;
								} else {
									JOptionPane.showMessageDialog(null,
											"1~45의 수가 아니거나 중복 입니다.");
									break;
								}
								c++;
							}
							if (!nu3.getText().equals("")) {
								int cc = Integer.parseInt(nu3.getText());
								if (cc > 0 && cc < 46
										&& !nu3.getText().equals(nu1.getText())
										&& !nu3.getText().equals(nu2.getText())) {
									a[c] = cc;
								} else {
									JOptionPane.showMessageDialog(null,
											"1~45의 수가 아니거나 중복 입니다.");
									break;
								}
								c++;
							}
							if (!nu4.getText().equals("")) {
								int cc = Integer.parseInt(nu4.getText());
								if (cc > 0 && cc < 46
										&& !nu4.getText().equals(nu1.getText())
										&& !nu4.getText().equals(nu2.getText())
										&& !nu4.getText().equals(nu3.getText())) {
									a[c] = cc;
								} else {
									JOptionPane.showMessageDialog(null,
											"1~45의 수가 아니거나 중복 입니다.");
									break;
								}
								c++;
							}
							if (!nu5.getText().equals("")) {
								int dd = Integer.parseInt(nu5.getText());
								if (dd > 0 && dd < 46
										&& !nu5.getText().equals(nu1.getText())
										&& !nu5.getText().equals(nu2.getText())
										&& !nu5.getText().equals(nu3.getText())
										&& !nu5.getText().equals(nu4.getText())) {
									a[c] = dd;
								} else {
									JOptionPane.showMessageDialog(null,
											"1~45의 수가 아니거나 중복 입니다.");
									break;
								}
								c++;
							}
							if (!nu6.getText().equals("")) {
								int ee = Integer.parseInt(nu6.getText());
								if (ee > 0 && ee < 46
										&& !nu6.getText().equals(nu1.getText())
										&& !nu6.getText().equals(nu2.getText())
										&& !nu6.getText().equals(nu3.getText())
										&& !nu6.getText().equals(nu4.getText())
										&& !nu6.getText().equals(nu5.getText())) {
									a[c] = ee;
								} else {
									JOptionPane.showMessageDialog(null,
											"1~45의 수가 아니거나 중복 입니다.");
									break;
								}
								c++;
							}
						}
						for (int i = c; i < 6; i++) {
							int number = (int) (Math.random() * 45 + 1);
							a[i] = number;

							for (int j = i - 1; j >= 0; j--) {
								if (a[i] == a[j]) {
									i--;
									break;
								}
							}
						}

						Arrays.sort(a);
						result.append(Arrays.toString(a) + " ");
						result.append("\n");

					} catch (Exception e) {
						JOptionPane.showMessageDialog(null, "숫자를 입력하시오.");
					}
				}
			});
			btnLotto_2.setBounds(109, 229, 85, 23);
		}
		return btnLotto_2;
	}

	private JButton getBtnLotto_3() {
		if (btnLotto_3 == null) {
			btnLotto_3 = new JButton("Lotto4");
			btnLotto_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					// 종대(5조)버전 - 입력값이 공백이 아닌경우 1~45범위값인지 체크
					int pos = 0; // 행운의 수가 대입될 위치.
					int[] base = new int[45];
					int[] lo = new int[6];
					for (int i = 1; i < 46; i++)
						base[i - 1] = i;
					String[] lu = { nu1.getText(), nu2.getText(),
							nu3.getText(), nu4.getText(), nu5.getText(),
							nu6.getText() };
					// 중복수 체크
					for (int i = 0; i < lu.length; i++) {
						for (int j = i + 1; j < lu.length; j++) {
							if (!lu[i].equals("") && lu[i].equals(lu[j])) {
								JOptionPane.showMessageDialog(null, "중복수가 있네요");
								return;
							}
						}
					}
					try {
						// 행운의 수를 lo배열의 앞에서부터 채우고
						// base배열의 값을 99로 변경
						for (int i = 0; i < lu.length; i++) {
							if (!lu[i].equals("")) {
								int temp = Integer.parseInt(lu[i]);
								if (temp > 0 && temp < 46) {
									lo[pos] = temp;
									base[temp - 1] = 99;
									pos++;
								} else {
									Exception ee = new Exception("눈이 삐꾸냐? 한글모르냐? 영어로 쳐주랴!!!");
									throw ee;
//									JOptionPane.showMessageDialog(null,
//											"눈이 삐꾸냐? 한글모르냐? 영어로 쳐주랴!!!");
//									return;

								}
							}
						}

						// base배열을 정렬한 후 행운의수를 제외한 나머지를 섞음.
						Arrays.sort(base);
						for (int i = 0; i < 1000; i++) {
							int k = (int) (Math.random() * (44 - pos)) + 1;
							int temp = base[0];
							base[0] = base[k];
							base[k] = temp;
						}

						System.arraycopy(base, 0, lo, pos, 6 - pos);
						Arrays.sort(lo);
						result.append(Arrays.toString(lo));
						result.append("\n");

						result.setCaretPosition(result.getText().length());

					} catch (Exception ex) {
						JOptionPane.showMessageDialog(null,
								"\n손가락을 확마 뿌려트려 버릴놈이...제대로 눌러라 죽기 싫으면...");
					}

				}
			});
			btnLotto_3.setBounds(206, 229, 85, 23);
		}
		return btnLotto_3;
	}
}
